#include<iostream> 
#include<vector> 

using namespace std;

int main(void)
{
	int i,j,n,f1,f2,fn;
	int A,B;
	vector<int> list;
	int start,end,u,v; 
	int re;

	for(cin>>A>>B>>n; A!=0||B!=0||n!=0; cin>>A>>B>>n) 
	{
		list.clear();
		fn = f1 = f2 = 1;
		re=-1;
		list.push_back(f1);
		list.push_back(f2);
	
		for(i=3; i<=n; i++)
		{
			fn = ( A*f2 + B*f1 ) % 7;
			list.push_back(fn);
			if(i>3)
			{
				for(j=0;j<list.size()-4;j++)
				{
					if(list[i-1]==list[j+1] && list[i-2]==list[j])
						break;
				}
				if(j<list.size()-4) re=j; 
			}
			if(re!=-1) break;
		      
			f1 = f2;
			f2 = fn;
		}
		if(i<=n)
			fn=list[(n-re-1)%(list.size()-2-re)+re];
		cout << fn << endl;
	}	
}
